package router

import (
	swaggerFiles "github.com/swaggo/files"
	"net/http"
	"purchase-mall/controllers"
	"purchase-mall/logger"
	"purchase-mall/middlewares"
	"time"

	ginSwagger "github.com/swaggo/gin-swagger"
	//"github.com/swaggo/gin-swagger/swaggerFiles"
	_ "purchase-mall/docs"

	"github.com/gin-contrib/pprof"
	"github.com/gin-gonic/gin"
)

// SetupRouter 路由
func SetupRouter(mode string) *gin.Engine {
	if mode == gin.ReleaseMode {
		gin.SetMode(gin.ReleaseMode) // gin设置成发布模式
	}
	r := gin.New()
	//r.Use(logger.GinLogger(), logger.GinRecovery(true), middlewares.RateLimitMiddleware(2*time.Second, 1))
	r.Use(logger.GinLogger(), logger.GinRecovery(true))
	//timeout  10min
	r.Use(middlewares.TimeoutMiddleware(time.Minute * 10))

	//r.LoadHTMLFiles("./templates/index.html")
	//r.Static("/static", "./static")
	//
	//r.GET("/", func(c *gin.Context) {
	//	c.HTML(http.StatusOK, "index.html", nil)
	//})

	r.GET("/ping", func(c *gin.Context) {
		c.String(http.StatusOK, "pong")
	})

	r.GET("/swagger/*any", ginSwagger.WrapHandler(swaggerFiles.Handler))

	v1 := r.Group("/go/v1")

	// 注册
	v1.POST("/signup", controllers.SignUpHandler)
	// 登录
	v1.POST("/login", controllers.LoginHandler)

	// 列表
	v1.GET("/table_list", controllers.TableListHandler)
	v1.GET("/token_list_ts", controllers.TokenListHandler)

	//loki
	v1.POST("/request_top20", controllers.RequestTop20Handler) // 请求总数排行 top20
	v1.GET("/peak_value", controllers.PeakValueHandler)        // 近三天请求峰值
	v1.POST("/token_list", controllers.TokenListByLokiHandler)
	v1.POST("/ip_list", controllers.IpListByLokiHandler)
	v1.GET("/access_total", controllers.AccessTotalHandler)
	v1.GET("/access_total_week", controllers.AccessTotalWeekHandler)

	v1.POST("/aliyun", controllers.AliYunHandler)
	v1.POST("/logs_total", controllers.LogstotalHandler)

	v1.Use(middlewares.JWTAuthMiddleware()) // 应用JWT认证中间件

	pprof.Register(r) // 注册pprof相关路由

	r.NoRoute(func(c *gin.Context) {
		c.JSON(http.StatusOK, gin.H{
			"msg": "404",
		})
	})
	return r
}
